const gulp = require('gulp')
const gutil = require('gulp-util')
const webpack = require('webpack')
const WebpackDevServer = require('webpack-dev-server')
const webpackConfig = require('./webpack.config')
const jade = require('gulp-jade')
const less = require('gulp-less')
const _ = require('underscore')
const jscs = require('gulp-jscs')
const uglify = require('gulp-uglify')
const replace = require('gulp-replace')

gulp.task('dev', ['b'], function (callback) {


  var myConfig = Object.create(webpackConfig)
  myConfig.devtool = 'eval'
  myConfig.debug = true

  var compiler = webpack(webpackConfig)

  new WebpackDevServer(compiler, {
    // server and middleware options
    contentBase: myConfig.output.contentBase,
    hot: true,
    // 设置代理
    // proxy: {
    //     '/some/path*': {
    //         target: 'https://other-server.example.com',
    //     },
    // },
    stats: {
      color: true
    }

  }).listen(8080, 'localhost', function (err) {

    if (err) throw new gutil.PluginError('webpack-dev-server', err)
    // Server listening
    gutil.log('[webpack-dev-server]', 'http://localhost:8080/webpack-dev-server/index.html')

    // keep the server alive or continue?
    callback()
  })

})


gulp.task('b', function (callback) {
  webpackConfig = require('./webpack.config')
  webpack(webpackConfig, function (err, stats) {
    if (err) throw new gutil.PluginError('webpack', err)
    gutil.log('[webpack]', stats.toString({
      // output options
    }))
    callback()
  })
})

gulp.task('jscs', function () {

  gulp.src('./src/js/**/*.js')
  .pipe(jscs({
    fix: true
  }))
  .pipe(jscs.reporter())
  .pipe(gulp.dest('./src/js/'))
})

gulp.task('default', ['b'])

// gulp.watch(['./src/js/**/*.js', './src/jade/tpls/*.jade'], ['b'])
// gulp.watch(['./src/less/**/*.less'], ['less'])
gulp.watch(['./src/vue/**/*.vue', './example/modules/**/*.vue', './example/**/**.js', '!./example/bundle.js'], ['b'])
